package generator.service.impl;

import com.github.davidfantasy.jwtshiro.JWTUserAuthService;
import com.github.davidfantasy.jwtshiro.UserInfo;
import generator.StuVo.StuVo;
import generator.biz.StuBiz;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
@RequiredArgsConstructor
public class JWTStuAuthServiceImpl implements JWTUserAuthService {
    @Autowired
    private StuBiz stuBiz;
    private final StringRedisTemplate stringRedisTemplate;
    @Override
    public UserInfo getUserInfo(String addr) {
        StuVo stuVo0WithRedis = stuBiz.getStuVoWithAddr(addr);
        UserInfo userInfo = new UserInfo();
        if (stuVo0WithRedis.getStuId() == null){
            return null;
        }
        userInfo.setAccount(stuVo0WithRedis.getAddress());
        userInfo.setSecret(stuVo0WithRedis.getStuName());
        return userInfo;
    }

    @Override
    public void onAuthenticationFailed(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
    }

    @Override
    public void onAuthorizationFailed(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        httpServletResponse.setStatus(HttpStatus.FORBIDDEN.value());
    }
}
